package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

public class CourseContentServiceImpl implements CourseContentService {
    CourseContentDao contentDao = new CourseContentDaoImpl();
    //查找课程内容
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int id) {
        List<Course_Section> sections = contentDao.findSectionAndLessonByCourseId(id);
        return sections;
    }
    //查找课程
    @Override
    public Course findCourseById(int courseId) {
        Course course = contentDao.findCourseByCourseId(courseId);
        return course;
    }

    //新增章节

    @Override
    public String saveSection(Course_Section section) {
        //1.补全章节信息
        section.setStatus(2);
        String date = DateUtils.getDateFormart();
        section.setCreate_time(date);
        section.setUpdate_time(date);
        //2.调用Dao进行插入
        int i = contentDao.saveSection(section);
        //3.根据插入是否成功，封装对应信息
        String result;
        if(i > 0){
            //保存成功
            result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            result = StatusCode.FAIL.toString();
            return result;
        }
    }

    // 修改章节信息

    @Override
    public String updateSection(Course_Section section) {
        //1.补全章节信息
        String date = DateUtils.getDateFormart();
        section.setUpdate_time(date);
        //2.调用Dao进行插入
        int i = contentDao.updateSection(section);
        //3.根据修改是否成功，封装对应信息
        String result;
        if(i > 0){
            //保存成功
            result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //修改章节状态

    @Override
    public String updateSectionStatus(int id, int status) {
        //调用Dao 修改章节的编辑状态
        int i = contentDao.updateSectionStatus(id,status);
        String result;
        //3.根据修改是否成功，封装对应信息
        if(i > 0){
            result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String saveLesson(Course_Lesson courseLesson) {
        //1.补全课程信息
        String date = DateUtils.getDateFormart();
        courseLesson.setCreate_time(date);
        courseLesson.setUpdate_time(date);
        //2.调用Dao进行插入
        int i = contentDao.saveLesson(courseLesson);
        if(i > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public String updateLesson(Course_Lesson courseLesson) {
        int i = contentDao.updateLesson(courseLesson);
        if(i > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else{
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }
}
